<?php

namespace App\V3\V3_2_6\Services;

use App\V3\V3_2_6\Models\HcMaterielTypeModel;
use App\V3\V3_2_6\Models\MaterielClassModel;
use App\V3\V3_2_6\Models\HcMaterielClassModel;
use App\V3\V3_2_6\Models\MaterielsTypeModel;
class HcMaterielTypeService
{
    /**
     * 新建商品信息
     */
    static public function add($data)
    {
        return HcMaterielTypeModel::insertGetId($data);
    }

    /**
     * 获取指定商品详细信息
     */
    static public function info($id)
    {
        return HcMaterielTypeModel::info($id);
    }

    /**
     * 物料列表
     */
    static public function HcmaterielTypeSelect($mainId, $keyword, $condition,$nextClassIdArr, $pageSize, $currentPage)
    {
        return HcMaterielTypeModel::materielTypeSelect($mainId, $keyword, $condition,$nextClassIdArr, $pageSize, $currentPage);
    }

    //选择物料商品列表
    static public function SelectMaterielType($mainId, $keyword, $condition, $pageSize, $currentPage)
    {
        return MaterielsTypeModel::select(
            'a.id',
            'materiel_name',
            'type_name',
            'materiel_img',
            'materiel_no',
            'materiel_class_id',
            'b.class_name as sec_class_name',
            'c.id as fir_class_id',
            'c.class_name as fir_class_name',
            'a.remark',
            'a.length',
            'a.width',
            'a.height',
            'a.weight',
            'a.attribute',
            'a.cost',
            'a.selling_price',
            'a.is_album'
            )
            ->from('materiel_type as a')
            ->leftJoin('materiel_class as b', 'b.id', '=', 'a.materiel_class_id')
            ->leftJoin('materiel_class as c', 'b.parent_id', '=', 'c.id')
            ->where($condition)
            ->where(function($query) use ($keyword) {
                $query->orWhere('a.materiel_name', 'like', '%'.$keyword.'%');
                $query->orWhere('a.type_name', 'like', '%'.$keyword.'%');
                $query->orWhere('a.materiel_no', 'like', '%'.$keyword.'%');
                $query->orWhere('b.class_name', 'like', '%'.$keyword.'%');
                $query->orWhere('c.class_name', 'like', '%'.$keyword.'%');
            })
            ->where('a.delete_flag', 0)
            ->where('a.main_id', $mainId)
            ->orderBy('a.id', 'DESC')
            ->paginate($pageSize,['*'], '',$currentPage)->toArray();

    }

    static public function withSearch($aData,$parentId, $data, $condition, $condition1, $keyword)
    {

        $firstClass = [];
        $secondClass = [];
     foreach($aData as $k => $v) {
             $firstClass[] = $v['fir_class_id'];
             $secondClass[] = $v['materiel_class_id'];
         }

        return HcMaterielClassModel::select(
            'id',
            'class_name',
            'class_no'
            )
            ->from('hc_materiel_class')
            ->where('main_id', $parentId)
            ->with('nextClass', function($query) use ($secondClass, $condition1, $keyword) {
                $query->select(
                    'id',
                    'class_name',
                    'class_no',
                    'parent_id'
                    )
                    ->where('delete_flag', 0)
                    ->with('nextHcType', function($query) use ($condition1, $keyword) {
                        $query->select("*")
                            ->where($condition1)
                            ->where('delete_flag', 0);

                    })
                    ->whereIn('id', array_unique($secondClass));
            })
            ->where('parent_id', 0)
            ->where('delete_flag', 0)
            ->whereIn('id', array_unique($firstClass))
            ->orderBy('id', 'asc')
            ->paginate($data['pageSize'],['*'], '',$data['currentPage'])->toArray();
    }
    //物料商品分类下拉列表
    static public function select($mainId,$parentClassIdArr)
    {
            return MaterielClassModel::select(
                'id', 'class_name', 'class_no'
                )->with('nextClass', function($query) {
                    $query->select(
                        'id', 'class_name', 'class_no', 'parent_id'
                        )->where('delete_flag', 0)->orderBy('id', 'desc');
                })->where([
                    'main_id' => $mainId,
                    'delete_flag' => 0,

                ])
                ->whereIn('id',$parentClassIdArr)
                ->orderBy('id', 'desc')->get();
    }

    //物料商品分类下拉列表
    static public function nextClass($mainId,$parentId)
    {

            return MaterielClassModel::select(
            'id'
            )->where([
                    'main_id' => $mainId,
                    'delete_flag' => 0,
                    'parent_id' => $parentId
                ])->orderBy('id', 'desc')->get()->toArray();

    }

    //根据id检索商品信息是否已存在
    static public function checkMateriel($mainId, $ids)
    {
        if(empty(HcMaterielTypeModel::where(['main_id' => $mainId])->whereIn('id',$ids)->get()->toArray()))
        {
            return false;
        }
        else {
            return true;
        }
    }

    static public function checkRandNo($data)
    {
        $checkMaterielNo = HcMaterielTypeModel::checkMaterielTypeCode($data);

        if(!empty($checkMaterielNo)) {
            return false;
        }
        else {
            return true;
        }
    }

    //检查除自身外是否有相同的商品信息
    static public function checkMaterielInfoExceptself($data)
    {
        $checkMaterielTypeInfo = HcMaterielTypeModel::where([
            'main_id' => $data['main_id'],
            'materiel_name' => $data['materiel_name'],
            'type_name' => $data['type_name'],
            'materiel_class_id' => $data['materiel_class_id'],
            'delete_flag' => 0,
        ])
        ->where('id', '!=', $data['id'])
        ->first();

        if(!empty($checkMaterielTypeInfo)) {
            return false;
        }
        else {
            return true;
        }
    }
}
